﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyDuAn.DataAccess;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using QuanLyDuAn.DTO;

namespace QuanLyDuAn.BUS
{
    class AdminController
    {
        dbAccess database = new dbAccess();
        DataSet ds;
        SqlCommand cmd;
        SqlDataAdapter da;
        SqlConnection conn;
        ArrayList list = new ArrayList();
        ArrayList listUser = new ArrayList();
        SqlDataReader reader,reader1;
        public ArrayList LoadAdmin()
        {
            Admin admin;
            conn = database.GetConnect();
            conn.Open();
            cmd = new SqlCommand("GetAdmin",conn);
            da = new SqlDataAdapter(cmd);
            reader = cmd.ExecuteReader();
            while(reader.Read())
            {
                String userName = reader.GetString(0);
                String password = reader.GetString(1);
                int permission = reader.GetInt32(2);
                admin = new Admin(userName,password,permission);
                list.Add(admin);
            }
            return list;
        }
        public ArrayList LoadAdminByUser(String username)
        {
            Admin admin;
            conn = database.GetConnect();
            conn.Open();
            cmd = new SqlCommand("getAdminByUser", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = username;
            da = new SqlDataAdapter(cmd);
            reader1 = cmd.ExecuteReader();
            while (reader1.Read())
            {
                String userName = reader1.GetString(0);
                String password = reader1.GetString(1);
                int permission = reader1.GetInt32(2);
                admin = new Admin(username, password, permission);
                listUser.Add(admin);
            }
            return listUser;
        }
        public  void ChangePass(String usename,String password)
        {
            conn = database.GetConnect();
            conn.Open();
            cmd = new SqlCommand("changePass", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = usename;
            cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = password;
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        }
    }

